﻿/*
 * Rappelz: Endless Odyssey - The first free open source Rappelz server emulator
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rappelz.GameServer.Network;
using System.Data.Common;
using System.Data;
using Rappelz.GameServer.Network.GameClient;

namespace Rappelz.GameServer.Database
{
    /// <summary>
    /// DBLoader_NPC
    /// </summary>
    public class DBLoader_NPC : IDBProc
    {
        GameDBManager db;
        public BaseObject m_CBObject = null;

        private enum DBFields : int
        {
            AccountId = 0,
        }

        public DBLoader_NPC(GameDBManager dbManager)
        {
            db = dbManager;
        }

        public void SetCB(BaseObject obj)
        {
            m_CBObject = obj;
        }

        public void RunCB()
        {
            if (m_CBObject != null)
                m_CBObject.onEndQuery();
        }

        public bool onProcess()
        {

            DbConnection conn = db.CreateConnection();
            DbCommand cmd = conn.CreateCommand();

            cmd.CommandText = "select * from NPCResource";
            cmd.CommandType = CommandType.Text;

            conn.Open();
            int count = 0;
            try
            {
                DbDataReaderTypeCastHelper dr = new DbDataReaderTypeCastHelper(cmd.ExecuteReader());
                while (dr.Read())
                {
                    int off = 0;
                    NPCBase npcbase = new NPCBase();
                    npcbase.id = dr.GetInt32(off++);
                    npcbase.text_id  = dr.GetInt32(off++);
                    npcbase.name_text_id  = dr.GetInt32(off++);
                    int race_id = dr.GetInt32(off++);
                    int sexsual_id  = dr.GetInt32(off++);
                    npcbase.x  = dr.GetInt32(off++);
                    npcbase.y  = dr.GetInt32(off++);
                    npcbase.z  = dr.GetInt32(off++);
                    npcbase.face  = dr.GetInt32(off++);
                    npcbase.local_flag = (uint)dr.GetInt32(off++);
                    npcbase.is_periodic = dr.GetByte(off++) != 0;
                    DateTime begin_of_period = dr.GetDateTime(off++);
                    npcbase.begin_of_period = (long)(begin_of_period - Globals.StartOfEpoch).TotalMilliseconds;
                    DateTime end_of_period = dr.GetDateTime(off++);
                    npcbase.end_of_period = (long)(end_of_period - Globals.StartOfEpoch).TotalMilliseconds;
                    int face_x = dr.GetInt32(off++);
                    int face_y = dr.GetInt32(off++);
                    int face_z = dr.GetInt32(off++);
                    string model_file = dr.GetString(off++);
                    int hair_id  = dr.GetInt32(off++);
                    int face_id  = dr.GetInt32(off++);
                    int body_id  = dr.GetInt32(off++);
                    int weapon_item_id  = dr.GetInt32(off++);
                    int shield_item_id  = dr.GetInt32(off++);
                    int clothes_item_id  = dr.GetInt32(off++);
                    int helm_item_id  = dr.GetInt32(off++);
                    int gloves_item_id  = dr.GetInt32(off++);
                    int boots_item_id = dr.GetInt32(off++);
                    int belt_item_id = dr.GetInt32(off++);
                    int mantle_item_id = dr.GetInt32(off++);
                    int necklace_item_id = dr.GetInt32(off++);
                    int earring_item_id = dr.GetInt32(off++);
                    int ring1_item_id = dr.GetInt32(off++);
                    int ring2_item_id = dr.GetInt32(off++);
                    int motion_id = dr.GetInt32(off++);
                    int is_roam = dr.GetInt32(off++);
                    npcbase.roaming_id  = dr.GetInt32(off++);
                    npcbase.standard_walk_speed  = dr.GetInt32(off++);
                    npcbase.standard_run_speed  = dr.GetInt32(off++);
                    npcbase.walk_speed  = dr.GetInt32(off++);
                    npcbase.run_speed  = dr.GetInt32(off++);
                    npcbase.attackable  = dr.GetByte(off++);
                    npcbase.offensive_type  = dr.GetByte(off++);
                    npcbase.spawn_type  = dr.GetInt32(off++);
                    npcbase.chase_range  = dr.GetInt32(off++);

                    npcbase.regen_time  = dr.GetInt32(off++);
                    npcbase.level  = dr.GetInt32(off++);
                    npcbase.stat_id  = dr.GetInt32(off++);
                    npcbase.attack_range  = dr.GetInt32(off++);
                    npcbase.attack_speed_type  = dr.GetInt32(off++);
                    npcbase.hp  = dr.GetInt32(off++);
                    npcbase.mp  = dr.GetInt32(off++);
                    npcbase.attack_point  = dr.GetInt32(off++);
                    npcbase.magic_point  = dr.GetInt32(off++);
                    npcbase.level  = dr.GetInt32(off++);
                    npcbase.defence  = dr.GetInt32(off++);
                    npcbase.magic_defence  = dr.GetInt32(off++);
                    npcbase.attack_speed  = dr.GetInt32(off++);
                    npcbase.magic_speed  = dr.GetInt32(off++);
                    npcbase.accuracy  = dr.GetInt32(off++);
                    npcbase.magic_accuracy  = dr.GetInt32(off++);
                    npcbase.magic_avoid  = dr.GetInt32(off++);
                    npcbase.ai_script  = dr.GetString(off++);
                    npcbase.contact_script  = dr.GetString(off++);
                    int texture_group = dr.GetInt32(off++);
                    int eventID = 0;// dr.GetInt32(off++);

                    if(eventID == 0 || eventID == GameRule.nCurrentEventID)
                        GameContent.RegisterNPCInfo(npcbase);
                    count++;

/*
                    int v1; // ebp@0
                    int v2; // esi@1
                    XEnvStruct *v3; // eax@2
                    XEnvStruct *v4; // eax@4
                    __int64 v5; // qax@7
                    __int64 v6; // qax@8
                    XEnvStruct *v7; // eax@10
                    int v8; // eax@10
                    unsigned int v9; // ecx@12
                    unsigned int v10; // edi@12
                    __int64 v11; // qax@12
                    signed int v12; // edi@15
                    unsigned int v13; // ecx@15
                    __int64 v14; // qax@20

                    if ((npcbase.local_flag & 0x20000000) == 0 || GameRule.bIsServiceServer)
                    {
                        if ((npcbase.local_flag & 0x40000000) == 0 || GameRule.bIsServiceServer)
                        {
                            if ((GameRule.nCurrentLocalFlag & npcbase.local_flag) == 0)
                            {
                                if (!npcbase.is_periodic )
                                {
                                    npcbase.begin_of_period = 0;
                                    npcbase.end_of_period = 0;
                                    GameContent.RegisterNPCInfo(npc);
                                    count++;
                                }
                                else
                                {
                                    *(v1 - 28) = npcbase.dtBeginOfPeriod.year - 1900;
                                    *(v1 - 32) = npcbase.dtBeginOfPeriod.month - 1;
                                    *(v1 - 36) = npcbase.dtBeginOfPeriod.day;
                                    *(v1 - 40) = npcbase.dtBeginOfPeriod.hour;
                                    *(v1 - 44) = npcbase.dtBeginOfPeriod.minute;
                                    *(v1 - 48) = npcbase.dtBeginOfPeriod.second;
                                    v5 = _mktime64((v1 - 48));
                                    npcbase.begin_of_period = v5;
                                    if (npcbase.begin_of_period (HIDWORD(v5) & v5) != -1 )
                                    {
                                        *(v1 - 28) = npcbase.dtEndOfPeriod.year - 1900;
                                        *(v1 - 32) = npcbase.dtEndOfPeriod.month - 1;
                                        *(v1 - 36) = npcbase.dtEndOfPeriod.day;
                                        *(v1 - 40) = npcbase.dtEndOfPeriod.hour;
                                        *(v1 - 44) = npcbase.dtEndOfPeriod.minute;
                                        *(v1 - 48) = npcbase.dtEndOfPeriod.second;
                                        v6 = _mktime64((v1 - 48));
                                        npcbase.end_of_period = v6;
                                        if ( (HIDWORD(v6) & v6) != -1 )
                                        {
                                            }
                                            if (GameRule.nPeriodicalNPCAdjustment != 0)
                                            {
    //                                             v9 = HIDWORD(npcbase.begin_of_period);
    //                                             v10 = LODWORD(npcbase.begin_of_period);
    //                                             v11 = GameRule.nPeriodicalNPCAdjustment;
                                                if (npcbase.begin_of_period <= GameRule.nPeriodicalNPCAdjustment)
                                                {
                                                    npcbase.begin_of_period = 0;
                                                }
                                                else
                                                {
                                                    npcbase.begin_of_period -= GameRule.nPeriodicalNPCAdjustment;
                                                }
                                                if (npcbase.end_of_period <= GameRule.nPeriodicalNPCAdjustment)
                                                {
                                                    npcbase.end_of_period = 0;
                                                }
                                                else
                                                {
                                                    npcbase.end_of_period -= GameRule.nPeriodicalNPCAdjustment;
                                                }
                                            }
                                            v14 = _time64(0);
                                            if (Globals.GetArTime() <= npcbase.end_of_period)
                                            {
                                                GameContent.RegisterNPCInfo(npc);
                                                count++;
                                            }
                                        }
                                    }

                                }
                            }
                        }

                    }
*/
                }
            }
            catch (System.Data.SqlClient.SqlException e)
            {
                Globals.Log.Warning("Can't load NPCResource database, Sql string: {0}", e.Message);
            }
            catch (Exception ex)
            {
                Globals.Log.Error("DBLoader_NPC.onProcess Exception: {0}", ex);
            }
            conn.Close();

            Globals.Log.Info("Total {0} NPCResource loaded...", count);
            return count != 0;
        }
    }

}
